package com.ids.actions;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.ids.bean.TeamAttendanceData;
import com.ids.service.TeamAttendanceApprovalService;
import com.ids.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TeamAttendanceApproval extends ActionSupport {

	private static final long serialVersionUID = 1L;

	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] date = request.getParameterValues("dated");
		Date attendanceDate = null;
		String[] comboboxStatusvalueString = request.getParameterValues("statusTypeId");
		int[] comboboxStatusvalueInt = convertStringArraytoIntArray(comboboxStatusvalueString);
		String[] comboboxAttendanceValueString = request.getParameterValues("attendanceTypeId");
		int[] comboboxAttendanceValueInt = convertStringArraytoIntArray(comboboxAttendanceValueString);
		String[] comboboxDependantAttendanceValueString = request.getParameterValues("dependantAttendanceTypeId");
		int[] comboboxDependantAttendanceValueInt = convertStringArraytoIntArray(comboboxDependantAttendanceValueString);
		String[] empvalueString = request.getParameterValues("empCode");
		String[] compOffvalueString = request.getParameterValues("compOffStatus");
		float[] compOffvalueFloat = convertStringArraytoFloatArray(compOffvalueString);
		List<TeamAttendanceData> teamAttendanceData = new ArrayList<TeamAttendanceData>();
		int i = 0;
		TeamAttendanceData teamAttendanceDataBean = null;
		while (i < date.length) {
			teamAttendanceDataBean = new TeamAttendanceData();
			attendanceDate = StringtoDate(date[i]);
			teamAttendanceDataBean.setDated(attendanceDate);
			teamAttendanceDataBean.setAttendanceTypeId(comboboxAttendanceValueInt[i]);
			teamAttendanceDataBean.setDependantAttendanceTypeId(comboboxDependantAttendanceValueInt[i]);
			teamAttendanceDataBean.setStatusId(comboboxStatusvalueInt[i]);
			teamAttendanceDataBean.setEmpCode(empvalueString[i]);
			teamAttendanceDataBean.setCompOffData(compOffvalueFloat[i]);
			teamAttendanceData.add(teamAttendanceDataBean);
			i++;
		}
		List<String> resultApprovedEntries = TeamAttendanceApprovalService
			.getInstance().insertApprovedEntries(teamAttendanceData);
		Map<String, Object> session = ActionContext.getContext().getSession();
		String errorMessages = "";
		for (String errorMsg : resultApprovedEntries) {
			errorMessages += errorMsg;
		}
		session.put(Constants.DATA_MANIPULATION_MESSAGE, errorMessages);
		resultApprovedEntries.clear();
		return SUCCESS;
	}

	private float[] convertStringArraytoFloatArray(String[] sarray) {
		if (sarray != null) {
		float intarray[] = new float[sarray.length];
		for (int i = 0; i < sarray.length; i++) {
			intarray[i] = Float.parseFloat(sarray[i]);
		}
		return intarray;
		}
		return null;
	}

	private Date StringtoDate(String d) {
		Date date = new Date();
		String arr[] = d.split("/");
		date.setMonth(Integer.parseInt(arr[0]) - 1);
		date.setDate(Integer.parseInt(arr[1]));
		date.setYear(Integer.parseInt(arr[2]) + 100);
		return date;
	}

	private int[] convertStringArraytoIntArray(String[] sarray)throws Exception {
		if (sarray != null) {
		int intarray[] = new int[sarray.length];
		for (int i = 0; i < sarray.length; i++) {
			intarray[i] = Integer.parseInt(sarray[i]);
		}
		return intarray;
		}
		return null;
	}
}
